//
//  GGTMapViewController.m
//  GGT
//
//  Created by 安静的美男子 on 14/10/27.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTMapViewController.h"
#import "GGTBusinessModel.h"
#import "GGTAppDelegate.h"
#import "GGTFind_BusinessesModel.h"
#import "GGTMyAnnotation.h"
#import "GGTGet_categories_with_businessesModel.h"
@interface GGTMapViewController () <DPRequestDelegate,UITableViewDataSource,UITableViewDelegate>
//筛选按钮:
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIButton *button3;
//赛选列表
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UITableView *subTableView;
@property (nonatomic, retain) UITableView *tableView2;
@property (nonatomic, retain) UITableView *subTableView2;
@property (nonatomic, retain) UITableView *autoTableView;
//赛选列表状态   NO为消失 YES为出现
@property (nonatomic, assign) BOOL tableViewState;
@property (nonatomic, assign) BOOL subTableViewState;
@property (nonatomic, assign) BOOL tableView2State;
@property (nonatomic, assign) BOOL subTableView2State;
@property (nonatomic, assign) BOOL autoTableViewState;
//清除按钮
@property (nonatomic, assign) UIButton *cleanButton;
//清除按钮状态
@property (nonatomic, assign) BOOL cleanButtonState;
//地图View
@property (nonatomic, retain) MKMapView *mapView;
//model 数组
@property (nonatomic, retain) NSMutableArray *modelArr;
//Annotation 数组
@property (nonatomic, retain) NSMutableArray *annotationArr;
//数据存放数组
@property (nonatomic, retain) NSMutableArray *tableViewArr;
@property (nonatomic, retain) NSMutableArray *subTableViewArr;
@property (nonatomic, retain) NSMutableArray *tableView2Arr;
@property (nonatomic, retain) NSMutableArray *subTableView2Arr;
@property (nonatomic, retain) NSArray *autoTableViewArr;
//解析标志
@property (nonatomic, assign) int flag;
//筛选按钮标志
@property (nonatomic, assign) int tableViewFlag;
//取按钮数据状态
@property (nonatomic, assign) BOOL buttonDateDtate;
//取模型状态
@property (nonatomic, assign) BOOL modelState;
//区域数据请求状态
@property (nonatomic, assign) BOOL areaState;
//类别数据请求状态
@property (nonatomic ,assign) BOOL kindState;
//排序方式
@property (nonatomic, assign) int sortKind;
//临时保存类别数据
@property (nonatomic, retain) NSString *tempKindDate;
//用于地理位置反编码
@property (nonatomic, retain) CLGeocoder* geocoder;
//定位当前城市
@property (nonatomic, retain) NSString * currentCity;
//城市获取状态
@property (nonatomic, assign) BOOL currentCityState;
@property (nonatomic, assign) BOOL isfirst;
//用户位置
@property (nonatomic, assign) float latitude;
@property (nonatomic, assign) float longitude;
//最后标记的位置
@property (nonatomic, assign) float lastLatitude;
@property (nonatomic, assign) float lastLongitude;
//检测是否有新的最后标记位置
@property (nonatomic, assign) BOOL newLastLocal;
@end

@implementation GGTMapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        //初始化状态: NO为消失 YES为出现
        self.tableViewState = NO;
        self.subTableViewState = NO;
        self.tableView2State = NO;
        self.subTableView2State = NO;
        self.autoTableViewState= NO;
        self.cleanButtonState = NO;
        self.buttonDateDtate = NO;
        self.modelState = NO;
        self.kindState = NO;
        self.areaState = NO;
        self.currentCityState = NO;
        self.newLastLocal = NO;
        self.isfirst = YES;
        self.currentCity = nil;
        self.sortKind = 0;
        //数组初始化
        self.modelArr = [NSMutableArray array];
        self.annotationArr = [NSMutableArray array];
        
        self.tableViewArr = [NSMutableArray array];
        self.subTableViewArr = [NSMutableArray array];
        self.tableView2Arr = [NSMutableArray array];
        self.subTableView2Arr = [NSMutableArray array];
        // self.autoTableViewArr = [NSMutableArray array];
        self.autoTableViewArr = @[@"默认距离",@"500米以内",@"1000米以内",@"1500米以内",@"2000米以内",@"2500米以内",@"3000米以内",@"3500米以内",@"4000米以内",@"4500米以内",@"5000米以内"];// 1 - 5000; 默认 1000
        //解析标志初始化
        self.flag = 0;
        self.tableViewFlag = 0;
        
    }
    return self;
}
//2014-11-05 23:19:50.880 GGT[1133:208210] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
/*在xcode6中 苹果地图得定位方法修改了，以前得不能用了
报错说明：Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
直接上解决办法：
在Supporting Files下得info.plis里面添加NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseDescription 两个String字段，value可以为空，也可以设置YES，不过我得问题还是不能解决，最终还是找到得了问题所在，就是info.plist中还需要包含Supported interface orientations 这个Array字段。然后运行就解决了
 */
/*
 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"12345"];
 [string setAttributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(0, string.length)];
 [label setAttributedText:string];
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建地图View
    
    self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 54, self.view.frame.size.width, self.view.frame.size.height - 54)];
    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"mapType"]isEqualToString:@"0"]) {
        self.mapView.mapType = MKMapTypeStandard;
        
    } else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"mapType"]isEqualToString:@"1"]){
        self.mapView.mapType = MKMapTypeSatellite;
    } else {
        self.mapView.mapType = MKMapTypeHybrid;
    }
        
    self.mapView.userLocation.title = @"当前位置";
    [self.view addSubview:self.mapView];
    //创建筛选按钮
    [self filterButton];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.imageView.image = [UIImage imageNamed:@"findme.png"];
    [self.mapView addSubview:button];
                        
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self selector:@selector(receiveNotification) name:@"mapType" object:nil];
    
}
- (void)receiveNotification
{
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"mapType"]isEqualToString:@"0"]) {
        self.mapView.mapType = MKMapTypeStandard;
        
    } else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"mapType"]isEqualToString:@"1"]){
        self.mapView.mapType = MKMapTypeSatellite;
    } else {
        self.mapView.mapType = MKMapTypeHybrid;
    }
}
//定位调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D location = [userLocation coordinate];
    
    //    self.longitudeLabel.text = [NSString stringWithFormat:@"%f",userLocation.coordinate.longitude];
    //    self.latitudeLabel.text = [NSString stringWithFormat:@"%f",userLocation.coordinate.latitude];
    
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 250, 250);
    static BOOL fuck = NO;
    if (fuck == NO) {
         [self.mapView setRegion:region animated:YES];
        fuck = YES;
    }
   
     self.latitude =self.mapView.userLocation.location.coordinate.latitude;
    self.longitude =self.mapView.userLocation.location.coordinate.longitude;
//    CLLocation *myLocation = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

//    //地理位置反编码
//    self.geocoder = [[CLGeocoder alloc]init];
//    [self.geocoder reverseGeocodeLocation: myLocation  completionHandler:^(NSArray*placemarks,NSError* error)
//     {
//         //NSLog(@"hffjv");
//         for (CLPlacemark* place in placemarks) {
//             //NSLog(@"hffjv");
//             NSLog(@"name %@",place.name); //位置
//             NSLog(@"thoroughfare %@",place.thoroughfare);//街道
//             //子街道
//             NSLog(@"subthorou	ghfare %@",place.subAdministrativeArea);
//             //市
//             NSLog(@"loclitity %@",place.locality);
//             //区
//             NSLog(@"subLocality %@",place.subLocality);
//             //国家
//             NSLog(@"country %@",place.country);
//             NSLog(@"hffjv");
//         }
//         
//     }];
    if (self.currentCity == nil && self.isfirst == YES) {
        NSString *url = @"v1/business/find_businesses";
        NSString *params = [NSString stringWithFormat:@"latitude=%f&longitude=%f",self.latitude,self.longitude];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        self.currentCityState = YES;
        self.isfirst = NO;
    
    }
    


    
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{

    if (self.buttonDateDtate == YES) {
        if (self.flag % 2 == 0) {
            NSMutableArray *city = [result objectForKey:@"cities"];
            for (NSDictionary *dic in city) {
                NSMutableArray *districts = [dic objectForKey:@"districts"];
                for (NSDictionary *districtsDic in districts) {
                    NSString *districts = [districtsDic objectForKey:@"district_name"];
                    NSMutableArray *neighborhoods = [districtsDic objectForKey:@"neighborhoods"];
                    [self.subTableViewArr addObject:neighborhoods];
                    [self.tableViewArr addObject:districts];
                }
            }
            [self.tableView reloadData];
            
            [self.subTableView reloadData];
            //获取筛选第二个按钮数据
            
            NSString *url = @"v1/metadata/get_categories_with_businesses";
            NSString *params = [NSString stringWithFormat:@"city=%@",self.currentCity];
            [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        } else {
            
            NSArray *tempArr = [result objectForKey:@"categories"];
            for (NSDictionary * tempDic in tempArr) {
                GGTGet_categories_with_businessesModel * tempModel = [[GGTGet_categories_with_businessesModel alloc]initWithDictionary:tempDic];
                [self.tableView2Arr addObject:tempModel.category_name];
                NSArray *tempSubArr = tempModel.subcategories;
                NSMutableArray *tempfinalArr = [NSMutableArray array];
                for (NSDictionary *tempSubDic in tempSubArr) {
                    GGTGet_categories_with_businessesModel *tempSubModel = [[GGTGet_categories_with_businessesModel alloc]initWithDictionary:tempSubDic];
                    [tempfinalArr addObject: tempSubModel.category_name];
                }
                [self.subTableView2Arr addObject:tempfinalArr];
            }
            self.buttonDateDtate =NO;
            [self.tableView2 reloadData];
            [self.subTableView2 reloadData];
        }
        self.flag++;
        
    }
    if (self.currentCityState == YES) {
        self.currentCity = [[[result objectForKey:@"businesses"]lastObject]objectForKey:@"city"];
        //这里有BUG
   
        //获取筛选第一个按钮数据

            if (self.tableViewArr.count == 0) {
                self.buttonDateDtate = YES;
                NSString *url2 = @"v1/metadata/get_regions_with_businesses";
                NSString *params2 = [NSString stringWithFormat:@"city=%@",self.currentCity];
                [[[GGTAppDelegate instance] dpapi] requestWithURL:url2 paramsString:params2 delegate:self];
                self.currentCityState = NO;
            }
    }
    if (self.modelState == YES) {
        
        //
        NSArray* array = [NSArray arrayWithArray:self.mapView.annotations];
        [self.mapView removeAnnotations:array];
        [self.annotationArr removeAllObjects];
        //[self.mapView reloadInputViews];
        [self.modelArr removeAllObjects];
        NSArray *tempArr = [result objectForKey:@"businesses"];
        for (NSDictionary  * businessDic in tempArr) {
            GGTFind_BusinessesModel *tempModel = [[GGTFind_BusinessesModel alloc]initWithDictionary:businessDic];
            [self.modelArr addObject:tempModel];
            
        }
        
        [self creatMyPoint];
        
        
        self.modelState = NO;
    }
    
}
//创建model数据
- (void)creatModleDate:(NSString *)str
{
    if (self.areaState == YES) {
        NSString *url = @"v1/business/find_businesses";
        NSString *params = [NSString stringWithFormat:@"city=%@&region=%@",self.currentCity,str ];
        
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        self.areaState = NO;
    }
    if (self.kindState == YES) {
        if (self.sortKind == 0) {
            float latitude =self.mapView.userLocation.location.coordinate.latitude;
            float longitude =self.mapView.userLocation.location.coordinate.longitude;
            NSString *url = @"v1/business/find_businesses";
            NSString *params = [NSString stringWithFormat:@"latitude=%f&longitude=%f&category=%@",latitude,longitude,str];
            [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
            
            self.kindState = NO;
        } else{
        float latitude =self.mapView.userLocation.location.coordinate.latitude;
        float longitude =self.mapView.userLocation.location.coordinate.longitude;
        NSString *url = @"v1/business/find_businesses";
        NSString *params = [NSString stringWithFormat:@"latitude=%f&longitude=%f&category=%@&radius=%d",latitude,longitude,str ,self.sortKind];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        
        self.kindState = NO;
        }
    }
    self.modelState = YES;
    
}


//创建标记
- (void)creatMyPoint
{
    
    for (GGTFind_BusinessesModel *tempModel in self.modelArr) {
        float templatitude = [tempModel.latitude floatValue];
        float templongitude = [tempModel.longitude floatValue];
        self.lastLatitude = templatitude;
        self.lastLongitude = templongitude;
        //创建CLLocation 设置经纬度
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:templatitude longitude:templongitude];
        CLLocationCoordinate2D coord = [loc coordinate];
        //创建标题
        NSString *titile = [NSString stringWithFormat:@"%@",tempModel.name];
        NSString *subTitile = [NSString stringWithFormat:@"%@",tempModel.address];
        GGTMyAnnotation *myPoint = [[GGTMyAnnotation alloc]initWithCoordinate:coord andTitle:titile andSubtitle:subTitile];
        [self.annotationArr addObject:myPoint];
        
        
        //[self.mapView addAnnotation:myPoint];
        self.newLastLocal = YES;
    }
    [self.mapView addAnnotations:self.annotationArr];
    
    CLLocation * myloc;
    //创建CLLocation 设置经纬度
    if (self.newLastLocal == YES) {
    myloc = [[CLLocation alloc]initWithLatitude:self.lastLatitude longitude:self.lastLongitude];
    } else {
    myloc = [[CLLocation alloc]initWithLatitude:self.latitude longitude:self.longitude];
    }
    CLLocationCoordinate2D mycoord = [myloc coordinate];
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mycoord, 2500, 2500);
    
    [self.mapView setRegion:region animated:YES];
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (tableView == self.tableView) {
        return  self.tableViewArr.count;
    } else if (tableView == self.subTableView) {
        return [[self.subTableViewArr objectAtIndex:self.tableViewFlag] count];
    } else if (tableView == self.tableView2) {
        return self.tableView2Arr.count;
    }else if (tableView == self.subTableView2) {
        return [[self.subTableView2Arr objectAtIndex:self.tableViewFlag] count];
    }else {
        return self.autoTableViewArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.tableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
        cell.textLabel.text = self.tableViewArr[indexPath.row];
        return cell;
    } else if (tableView == self.subTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subTableView"];
        cell.textLabel.text = [self.subTableViewArr objectAtIndex:self.tableViewFlag][indexPath.row];
        
        return cell;
    } else if (tableView == self.tableView2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView2"];
        cell.textLabel.text = self.tableView2Arr[indexPath.row];
        return cell;
    } else if (tableView == self.subTableView2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subTableView2"];
        cell.textLabel.text = [self.subTableView2Arr objectAtIndex:self.tableViewFlag][indexPath.row];
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"autoTableView"];
        cell.textLabel.text = self.autoTableViewArr[indexPath.row];
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.tableView) {
        [self.subTableView removeFromSuperview];
        
        self.subTableView = [[UITableView alloc]initWithFrame:CGRectMake(160, 54, 160, 290) style:UITableViewStylePlain];
        self.subTableView.delegate = self;
        self.subTableView.dataSource = self;
        [self.view addSubview:self.subTableView];
        [self.subTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subTableView"];
        
        self.tableViewFlag = (int)indexPath.row;
        self.subTableViewState = YES;
    }
    if (tableView == self.tableView2) {
        [self.subTableView2 removeFromSuperview];
        
        self.subTableView2 = [[UITableView alloc]initWithFrame:CGRectMake(160, 54, 160, 290) style:UITableViewStylePlain];
        self.subTableView2.delegate = self;
        self.subTableView2.dataSource = self;
        [self.view addSubview:self.subTableView2];
        [self.subTableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subTableView2"];
        self.tableViewFlag = (int)indexPath.row;
        self.subTableView2State = YES;
    }
    if (tableView == self.subTableView) {
        NSLog(@"%@",[self.subTableViewArr objectAtIndex:self.tableViewFlag][indexPath.row]);
        
        self.areaState = YES;
        [self creatModleDate:[self.subTableViewArr objectAtIndex:self.tableViewFlag][indexPath.row]];
        [self cleanView];
    }
    if (tableView == self.subTableView2) {
        NSLog(@"%@",[self.subTableView2Arr objectAtIndex:self.tableViewFlag][indexPath.row]);
        self.kindState = YES;
        self.tempKindDate =[self.subTableView2Arr objectAtIndex:self.tableViewFlag][indexPath.row];
        [self creatModleDate:self.tempKindDate];
        [self cleanView];
    }
    if (tableView == self.autoTableView) {
        NSLog(@"%@",self.autoTableViewArr[indexPath.row]);
        self.sortKind = (int)indexPath.row * 500;
        if (self.tempKindDate == nil) {
            
        } else {
            [self creatModleDate:self.tempKindDate];
            self.kindState = YES;
        }
        [self cleanView];
    }
    
}
//创建筛选按钮
- (void)filterButton
{
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(0, 24, self.view.frame.size.width / 3 - 1 , 30);
    self.button1.layer.cornerRadius = 10;
    self.button1.layer.borderWidth = 0.5;
    [self.button1 setTitle:@"全部商区" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button1];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(self.view.frame.size.width / 3 + 1, 24, self.view.frame.size.width / 3 - 1 , 30);
    self.button2.layer.cornerRadius = 10;
    self.button2.layer.borderWidth = 0.5;
    [self.button2 setTitle:@"全部分类" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button2];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(self.view.frame.size.width / 3 * 2 + 2, 24, self.view.frame.size.width / 3 - 1 , 30);
    self.button3.layer.cornerRadius = 10;
    self.button3.layer.borderWidth = 0.5;
    [self.button3 setTitle:@"默认距离" forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button3];
    
}

//清空筛选按钮创建的视图
- (void)cleanView
{
    if (self.tableViewState == YES) {
        [self.tableView removeFromSuperview];
        self.tableViewState = NO;
    }
    if (self.subTableViewState == YES) {
        
        [self.subTableView removeFromSuperview];
        self.subTableViewState = NO;
    }
    if (self.tableView2State == YES) {
        [self.tableView2 removeFromSuperview];
        self.tableView2State = NO;
    }
    if (self.subTableView2State == YES) {
        [self.subTableView2 removeFromSuperview];
        self.subTableView2State = NO;
    }
    if (self.cleanButtonState == YES) {
        
        [self.cleanButton removeFromSuperview];
        self.cleanButtonState = NO;
    }
    if (self.autoTableViewState == YES) {
        [self.autoTableView removeFromSuperview];
        self.autoTableViewState = NO;
    }
}
- (void)creatCleanButton
{
    self.cleanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cleanButton.frame = CGRectMake(0, 24, self.view.frame.size.width, self.view.frame.size.height - 24);
    self.cleanButton.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.5];
    [self.cleanButton addTarget:self action:@selector(cleanButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cleanButton];
    self.cleanButtonState = YES;
    
}
- (void)cleanButtonClicked:(UIButton *)button
{
    [self cleanView];
}
- (void)button1Clicked:(UIButton *)button
{
    
    if (self.tableViewState == NO) {
        [self creatCleanButton];
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 160, 290) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource =self;
        self.tableView.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.tableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.tableView];
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"tableView"]];
        [UIView animateWithDuration:.1 animations:^{
            self.tableView.frame = CGRectMake(0, 54, 160, 290);
        }];
        
        self.tableViewState = YES;
    } else {
        [self cleanView];
    }
}
- (void)button2Clicked:(UIButton *)button
{
    
    if (self.tableView2State == NO) {
        [self creatCleanButton];
        self.tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 160, 290) style:UITableViewStylePlain];
        self.tableView2.delegate = self;
        self.tableView2.dataSource =self;
        self.tableView2.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.tableView2.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.tableView2];
        [self.tableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"tableView2"]];
        [UIView animateWithDuration:.1 animations:^{
            self.tableView2.frame = CGRectMake(0, 54, 160, 290);
        }];
        self.tableView2State = YES;
    } else {
        [self cleanView];
    }
}
- (void)button3Clicked:(UIButton *)button
{
    
    if (self.autoTableViewState == NO) {
        [self creatCleanButton];
        self.autoTableView = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 280, 290) style:UITableViewStylePlain];
        self.autoTableView.delegate = self;
        self.autoTableView.dataSource =self;
        self.autoTableView.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.autoTableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.autoTableView];
        [self.autoTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"autoTableView"]];
        [UIView animateWithDuration:.1 animations:^{
            self.autoTableView.frame = CGRectMake(0, 54, 280, 290);
        }];
        self.autoTableViewState = YES;
    } else {
        [self cleanView];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
